package com.aliya.glide

import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.widget.ImageView.ScaleType
import androidx.appcompat.app.AppCompatActivity
import com.aliya.glide.databinding.ActivityTransformBinding
import com.aliya.glide.ktx.viewBindings
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

/**
 * 演示 CenterCrop 与 RoundedCorners 同时使用异常问题
 */
class TransformActivity : AppCompatActivity() {
    companion object {
        const val TAG = "TransformActivity"
    }

    private val viewBindings by viewBindings(ActivityTransformBinding::bind)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transform)
        viewBindings.ivImage.apply {
            setOnClickListener {
                if (drawable != null) {
                    Log.e(
                        TAG,
                        "ivImage w:${it.width},h:${it.height} - drawable w:${drawable.minimumWidth},h:${drawable.minimumHeight}"
                    )
                } else {
                    // 正确顺序：先裁剪后圆角
                    val options = RequestOptions().transform(
                        MultiTransformation(object : CenterCrop() {
                            override fun transform(
                                pool: BitmapPool,
                                toTransform: Bitmap,
                                outWidth: Int,
                                outHeight: Int
                            ): Bitmap {
                                Log.e(TAG, "transform: " + Thread.currentThread().name)
                                return super.transform(pool, toTransform, outWidth, outHeight)
                            }
                        }, RoundedCorners(dimen(R.dimen.corner_20)))

                    )

                    Glide.with(this).load(R.mipmap.scale_20x10).apply(options)
                        .into(this)
                }
            }
        }

        viewBindings.ivImage0.apply {
            setOnClickListener {
                scaleType = ScaleType.CENTER_CROP
                if (drawable != null) {
                    Log.e(
                        TAG,
                        "ivImage0 w:${it.width},h:${it.height} - drawable w:${drawable.minimumWidth},h:${drawable.minimumHeight}"
                    )
                } else {
                    // 圆角失效
                    Glide.with(this).load(R.mipmap.scale_20x10)
                        .transform(RoundedCorners(dimen(R.dimen.corner_20)))
                        .into(this)
                }
            }
        }
        val imageView = viewBindings.ivImage0

        imageView.scaleType = ScaleType.CENTER_CROP
        Glide.with(imageView).load(R.mipmap.scale_20x10)
            .transform(RoundedCorners(dimen(R.dimen.corner_20)))
            .into(imageView)
    }

    private fun dimen(id: Int) = resources.getDimensionPixelSize(id)
}